package com.yogurt.ucenter.controller;


import com.baomidou.mybatisplus.extension.api.R;
import com.yogurt.ucenter.entity.Member;
import com.yogurt.ucenter.service.MemberService;
import com.yogurt.ucenter.vo.LoginVo;
import com.yogurt.ucenter.vo.RegisterVo;
import com.yogurt.utils.JwtUtils;
import com.yogurt.utils.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author yogurt
 * @since 2020-12-20
 */
@RestController
@RequestMapping("/ucenter/member")
@CrossOrigin
public class MemberController {

    @Autowired
    private MemberService memberService;

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public Result login(@RequestBody LoginVo vo){
        String token = memberService.login(vo);
        return Result.ok().data("token",token);
    }

    @ApiOperation("用户注册")
    @PostMapping("/register")
    public Result register(@RequestBody RegisterVo vo){
        memberService.register(vo);
        return Result.ok();
    }

    @ApiOperation("根据token获取用户信息")
    @GetMapping("/getMemberInfo")
    public Result getMemberInfo(HttpServletRequest request){
        //调用jwt工具类种的方法,通过request对象获取头信息,返回用户id
        String id = JwtUtils.getMemberIdByJwtToken(request);
        //根据id查询用户信息
        Member member = memberService.getById(id);
        return Result.ok().data("userInfo",member);
    }

    @ApiOperation("根据日期查询当天的注册人数")
    @GetMapping("/countRegister/{date}")
    public Result countRegister(@PathVariable String date) {
        int count = memberService.countRegister(date);
        return Result.ok().data("countRegister", count);
    }
}

